package com.youyiwen.Controller;

import com.youyiwen.Bean.Question;
import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.QuestionMapper;
import com.youyiwen.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/29
 */
@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @GetMapping("/userCenter")
    public String userCenter(HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginUser");
        String userName = user.getUserName();
        if (userName == null) {
            model.addAttribute("msg", "请先登录");
            return "/login";
        }
        List<Question> myQuestions = questionMapper.selectQuestionsByUserName(userName);
        model.addAttribute("myQuestions",myQuestions);

        return "userCenter";
    }

    @PostMapping("/updateInfo")
    public String updateInfo(@RequestParam("imgUrl") MultipartFile file,
                             @RequestParam("userName") String userName,
                             @RequestParam("telephone") String telephone,
                             @RequestParam("email") String email,
                             @RequestParam("userC1") String userC1,
                             @RequestParam("userC2") String userC2,
                             @RequestParam("userC3") String userC3,
                             @RequestParam("city") String city,
                             @RequestParam("changeCity") String changeCity,
                             @RequestParam("name") String name,
                             HttpSession session) {
        String updateCity;
        if ("change".equals(changeCity)){
            updateCity = userC1+" - "+userC2+" - "+userC3;
        }else{
            updateCity = city;
        }

        if (file.isEmpty()) {
            User user =(User) session.getAttribute("loginUser");
            String fileUrl = user.getImgUrl();
            userMapper.updateInfo(name,telephone, email, updateCity, fileUrl, userName);
            return "redirect:/userCenter";
        }
        String path = "D:/新建文件夹/springboot2/youyiwen1/src/main/resources/static/UserAvatar";

        String type = file.getOriginalFilename().split("\\.")[1];

        String fileName = userName + "Avatar." + type;

        String url = "/UserAvatar/" + fileName;
        File dest = new File(path + "/" + fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            userMapper.updateInfo(name,telephone, email, updateCity, url, userName);
            return "redirect:/userCenter";
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return "redirect:/userCenter";
        } catch (IOException e) {
            e.printStackTrace();
            return "redirect:/userCenter";
        }
    }
}
